package com.cms.server.resolver.plugins;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

public class CMSPSHTFormEndFlow extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFDataSet                    flowDataSet = EFDataSet.getInstance("FLOW_TASK_LIST");
		EFRowSet                      flowRowSet;
		EFDataSet                    itemDataSet = EFDataSet.getInstance("PSHTMX");
		String                            strSql = "";
		ResultSet                             rs = null;
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = "";

		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_NEXT, "N4");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "submit");
		
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
			   + " FLOW_ID = 'PSHT_EDIT_FLOW' and TASK_STATUS = 'ending'"
			   + " order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet = DataSetUtils.resultSet2DataSet(rs, flowDataSet);
		flowRowSet = flowDataSet.getRowSet(0);
		
		//如果不是开始节点，则需要更新上一个流程节点任务信息，同时还要插入一条新的记录
		FLOW_ID = flowRowSet.getString("FLOW_ID", "");

		//单据分录的部门
		strSql = " select * from PSHTMX where " + PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH) + " = '" + F_DJBH + "' and "
		       + PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ) + " = '" + F_KJQJ + "' ";
		rs = stmt.executeQuery(strSql);
		itemDataSet = DataSetUtils.resultSet2DataSet(rs, itemDataSet);
		
		if(itemDataSet.getRowCount() == flowDataSet.getRowCount()) {
			strSql = "insert into FLOW_TASK_END_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME," 
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS,RESR_IN_CAUSE," 
				   + "RESR_OUT_CAUSE,";
			for(int i = 1; i <= 20; i++) {
				if(i < 10) strSql += "F_CHAR0" + i + ",F_NUM0" + i + ",F_DATE0" + i + ",";
				else strSql += "F_CHAR" + i + ",F_NUM" + i + ",F_DATE" + i + ",";
			}
			strSql += "TASK_TO_UNIT,TASK_TO_UNIT_NAME,TASK_TO_USER,TASK_TO_USER_NAME) select "
				    + "OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
				    + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS,RESR_IN_CAUSE," 
				    + "RESR_OUT_CAUSE,";
			for(int i = 1; i <= 20; i++) {
				if(i < 10) strSql += "F_CHAR0" + i + ",F_NUM0" + i + ",F_DATE0" + i + ",";
				else strSql += "F_CHAR" + i + ",F_NUM" + i + ",F_DATE" + i + ",";
			}
			strSql += "TASK_TO_UNIT,TASK_TO_UNIT_NAME,TASK_TO_USER,TASK_TO_USER_NAME from FLOW_TASK_LIST where BIZ_DJBH = '" + F_DJBH + "' and BIZ_DATE = '"
			        + F_KJQJ + "' and BIZ_GUID = '" + F_GUID + "' and FLOW_ID = '" + FLOW_ID + "'";
			stmt.execute(strSql);
			
			strSql = "delete from FLOW_TASK_LIST  where BIZ_DJBH = '" + F_DJBH + "' and BIZ_DATE = '"
			       + F_KJQJ + "' and BIZ_GUID = '" + F_GUID + "' and FLOW_ID = '" + FLOW_ID + "'";
			stmt.execute(strSql);
			
			strSql = "update PSHTXX set F_DJZT = '3' where F_DJBH = '" + F_DJBH + "' and F_KJND = '"
			       + F_KJQJ + "' and F_GUID = '" + F_GUID + "'";
			stmt.execute(strSql);
		}
	}
}
